package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * Author  kkw
 * Date  2021/6/22 21:31
 * Version 1.0
 * Description :
 */
@Slf4j
@RestController
@RequestMapping("shopping")
public class OrderController {

    @Reference(timeout = 3000,check = false)
    OrderCoreService orderCoreService;

    @Reference(timeout = 3000,check = false)
    OrderQueryService orderQueryService;

    /**
     * 创建订单
     * @param request
     * @param req
     * @return
     */
    @PostMapping("order")
    public ResponseData order(@RequestBody CreateOrderRequest request, HttpServletRequest req) {
        String userInfo = ((String) req.getAttribute(TokenIntercepter.USER_INFO_KEY));
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
       request.setUserId(uid);
       request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse order = orderCoreService.createOrder(request);
        if (OrderRetCode.SUCCESS.getCode().equals(order.getCode())) {
            return new ResponseUtil<>().setData(order.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(order.getMsg());
    }


    /**
     * 查询所有订单
     * @param request
     * @param servletRequest
     * @return
     */
    @GetMapping("order")
    public ResponseData queryOrder(OrderListRequest request, HttpServletRequest servletRequest) {
        String userInfo = (String)servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long userId = Long.parseLong(object.get("uid").toString());
        request.setSort("create_time");
        request.setUserId(userId);
        OrderListResponse response = orderQueryService.queryOrder(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 根据orderID查询订单
     * @param orderId
     * @return
     */
    @GetMapping("order/{orderId}")
    public ResponseData detail(@PathVariable String orderId) {
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(orderId);
        OrderSingleDetailResponse orderDetailResponse = orderQueryService.orderDetailById(orderDetailRequest);
        if (OrderRetCode.SUCCESS.getCode().equals(orderDetailResponse.getCode())){
            System.out.println(orderDetailResponse);
            return new ResponseUtil<>().setData(orderDetailResponse);
        }
        return new ResponseUtil<>().setErrorMsg(orderDetailResponse.getMsg());
    }

    /**
     * 取消订单
     * @param request
     * @return
     */
    @PostMapping("cancelOrder")
    public ResponseData postCancelOrder(@RequestBody CancelOrderRequest request){

        CancelOrderResponse response = orderCoreService.cancelOrder(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
